﻿using System;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using LiSAClient.Core.Interfaces;
using LiSAClient.Core.Messages;
using LiSAClient.Core.Models;
using LiSAClient.Core.Services;
using LiSAClient.Core.Services.Factories;
using LiSAClient.Core.Viewmodel;

namespace LiSAClient.WpfClient.ViewModel.Users
{
    public class NewUserViewModel : ContentViewModel, ICommitable
    {
        #region Observable Objects

        private string warningText;
        public string WarningText
        {
            get
            {
                return this.warningText;
            }

            set
            {
                this.Set(() => this.WarningText, ref this.warningText, value);

            }
        }

        private bool canCreate;
        public bool CanCreate
        {
            get
            {
                return this.canCreate;
            }

            set
            {
                this.Set(() => this.CanCreate, ref this.canCreate, value);

            }
        }
            
        public ICommand Commit { get; private set; }

        public UserInformationFields UserInfo { get; set; }
        #endregion

        #region Initializers

        public NewUserViewModel()
        {
            this.canCreate = false;
            this.Commit = new RelayCommand(this.DoCreateNewUser);
            this.UserInfo = new UserInformationFields();
        }

        public override void OnPresented()
        {
            this.UserInfo.CurrentUser.UserName = "NewUser";
            this.UserInfo.CurrentUser.NewEncryptedPassword = "1234";
        }
        #endregion

        #region Methods

        /// <summary>
        /// Creates a new User if necessary information is filled out.
        /// </summary>
        private void DoCreateNewUser()
        {
            this.UserInfo.CurrentUser.FullName = this.UserInfo.CurrentUser.FullName.Trim();
            this.UserInfo.CurrentUser.Organization.Name = this.UserInfo.CurrentUser.Organization.Name.Trim();
            this.UserInfo.CurrentUser.Organization.Country = this.UserInfo.CurrentUser.Organization.Country.Trim();

            CheckMandatoryFields();
            if (!canCreate)
            {
                Messenger.Default.Send(new ShowNotificationMessage
                {
                    NotificationType = NotificationType.Warning,
                    Header = "Warning!",
                    Body = "Mandatory fields are not filled out. Please fill them out before continuing."
                });

                return;
            }

            this.UserInfo.CurrentUser.FullName = this.UserInfo.CurrentUser.Person.FirstName + " " +
                                                 this.UserInfo.CurrentUser.Person.MiddleName + " " +
                                                 this.UserInfo.CurrentUser.Person.LastName;
            ServiceFactory.Instance.Get<UserService>().CreateUser(this.UserInfo.CurrentUser);

            // Close the window
            if (this.Presenter != null)
            {
                this.Presenter.SendClosingRequest();
            }
        }

        /// <summary>
        /// Ensures mandatory fields are filled out correctly
        /// </summary>
        private void CheckMandatoryFields()
        {
            this.WarningText = "";
            if (String.IsNullOrWhiteSpace(this.UserInfo.CurrentUser.UserName))
            {
                this.CanCreate = false;
            }
            if (String.IsNullOrWhiteSpace(this.UserInfo.CurrentUser.NewEncryptedPassword) || this.UserInfo.CurrentUser.NewEncryptedPassword.Length < 4)
            {
                this.WarningText += "Password must be at least 4 characters";
                this.CanCreate = false;
            }

            if (String.IsNullOrWhiteSpace(this.UserInfo.CurrentUser.Person.FirstName))
            {
                this.CanCreate = false;
            }

            if (String.IsNullOrWhiteSpace(this.UserInfo.CurrentUser.Person.LastName))
            {
                this.CanCreate = false;
            }

            if (String.IsNullOrWhiteSpace(this.UserInfo.CurrentUser.Email))
            {
                this.WarningText += "You must submit an E-mail";
                this.CanCreate = false;
            }
            this.CanCreate = true;
        }

        /// <summary>
        /// Ensures the UserName is unique
        /// </summary>
        private void CheckUsername()
        {
            var isFree = ServiceFactory.Instance.Get<UserService>().ValidateUsername(this.UserInfo.CurrentUser.UserName);
            if (!isFree)
            {
                this.WarningText = "That username is not available.";
                this.CanCreate = false;
            }
            else
            {
                this.WarningText = "";
                this.CanCreate = true;
            }
        }
        #endregion
    }
}